// 工具箱
import ToolsFactory from './toolsFactory'

// 根据数据绘制转孔信息
export default class BatchTurnHole {
    constructor() {
        this.length = 0;
        this.index = 0;
        this.dataList = [];
        this.DataSource = null;
        this.ToolsFactory = new ToolsFactory()
    }
    delDataSource() {
        this.DataSource && viewer.dataSources.remove(this.DataSource);
    }
    init(data) {
        this.index = 0;
        this.dataList = [];
        this.dataList = data;
        viewer.scene.globe.depthTestAgainstTerrain = false;
        viewer.scene.fxaa = false
        viewer.scene.postProcessStages.fxaa.enabled = false;
        var supportsImageRenderingPixelated = viewer.cesiumWidget._supportsImageRenderingPixelated;
        if (supportsImageRenderingPixelated) {
            var vtxf_dpr = window.devicePixelRatio;
            while (vtxf_dpr >= 2.0) { vtxf_dpr /= 2.0; }
            viewer.resolutionScale = vtxf_dpr;
        }

        this.DataSource = null;
        // viewer.dataSources.removeAll();
        this.DataSource = this.DataSource || new Cesium.CustomDataSource('turnEntities');

        // 数据处理
        // const proList = [];
        // this.dataList.forEach(item => {
        //     proList.push(this.ToolsFactory.getCoordinatesByLonLat(item.coordinates[0], item.coordinates[1]))
        // })
        if (this.dataList.length > 0) {
            this.drawSingleEntity(this.dataList[this.index])
        }
        viewer.dataSources.add(this.DataSource);
        // return this.DataSource
    }
    SourceshowHide(t) {
        const poOBjArrsource = viewer.dataSources.getByName('turnEntities') || [];

        if (poOBjArrsource[0]) {
            poOBjArrsource[0].entities.values.forEach(ent => {
                ent.show = t ? t : !ent.show
            })
        }


        // poOBjArrsource[0] && poOBjArrsource.forEach(item => {
        //     item.show = t;
        // })
        // debugger
        // if (viewer.dataSources.getByName("turnEntities")[0]) {
        //     viewer.dataSources.getByName("turnEntities")[0].entities.values.forEach(en => {
        //         en.show = t
        //     })
        // }
    }
    drawSingleEntity(target) {
        this.ToolsFactory.getHeigthByLonLat(target.coordinates[0], target.coordinates[1]).then(resp => {
            target.coordinates.push(resp)
            this.DataSource.entities.add({
                name: "zhuankong",
                id: 'zhuankong' + target.eventid,
                position: Cesium.Cartesian3.fromDegrees(target.coordinates[0], target.coordinates[1], resp),
                label: {
                    text: target.name,
                    color: Cesium.Color.fromCssColorString('#fff'),
                    font: 'normal 16px MicroSoft YaHei',
                    showBackground: true,
                    outlineColor: new Cesium.Color(0.0, 0.0, 0.0, 1.0),
                    outlineWidth: 2.0,
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                    scale: 1,
                    pixelOffset: new Cesium.Cartesian2(0.0, -1.0),
                    pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.5e2, 50, 8.0e6, 1.0),//根据距照相机的距离指定广告牌的像素偏移
                    distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 10000.0),
                },
                billboard: {
                    image: require("../../assets/materialImg/turn1.png"),  // "../../../asset/materialImg/line-markder.png",
                    width: 32,
                    height: 32,
                    sizeInMaters: true,
                }
            });
            if (this.index <= this.dataList.length - 1) {
                this.index++;
                this.dataList[this.index] && this.drawSingleEntity(this.dataList[this.index])
            }
            viewer.flyTo(this.DataSource);
        })
    }
    init1(positons, target) {
        //地质开挖不能有深度测试，有深度测试会被遮盖
        viewer.scene.globe.depthTestAgainstTerrain = false;
        viewer.scene.fxaa = false
        viewer.scene.postProcessStages.fxaa.enabled = false;
        var supportsImageRenderingPixelated = viewer.cesiumWidget._supportsImageRenderingPixelated;
        if (supportsImageRenderingPixelated) {
            var vtxf_dpr = window.devicePixelRatio;
            while (vtxf_dpr >= 2.0) { vtxf_dpr /= 2.0; }
            viewer.resolutionScale = vtxf_dpr;
        }

        this.DataSource = null;
        // viewer.dataSources.removeAll();
        this.DataSource = this.DataSource || new Cesium.CustomDataSource('turnEntities');

        for (let i = 0; i < positons.length; i++) {
            const dat = positons[i];
            this.DataSource.entities.add({
                name: "zhuankong",
                id: 'zhuankong' + target[i].eventid,
                position: Cesium.Cartesian3.fromDegrees(dat.lon, dat.lat, dat.height - 16),
                label: {
                    text: target[i].name,
                    color: Cesium.Color.fromCssColorString('#fff'),
                    font: 'normal 16px MicroSoft YaHei',
                    showBackground: true,
                    outlineColor: new Cesium.Color(0.0, 0.0, 0.0, 1.0),
                    outlineWidth: 2.0,
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                    scale: 1,
                    pixelOffset: new Cesium.Cartesian2(0.0, -1.0),
                    pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.5e2, 50, 8.0e6, 1.0),//根据距照相机的距离指定广告牌的像素偏移
                    distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 10000.0),
                },
                billboard: {
                    name: target[i].name,
                    image: require("../../assets/materialImg/turn1.png"),  // "../../../asset/materialImg/line-markder.png",
                    width: 32,
                    height: 32,
                    sizeInMaters: true,
                }

            });
        }

        viewer.dataSources.add(this.DataSource);
        viewer.flyTo(this.DataSource);
    }
    init2(positons, target) {
        //地质开挖不能有深度测试，有深度测试会被遮盖
        viewer.scene.globe.depthTestAgainstTerrain = false;
        viewer.scene.fxaa = false
        viewer.scene.postProcessStages.fxaa.enabled = false;
        var supportsImageRenderingPixelated = viewer.cesiumWidget._supportsImageRenderingPixelated;
        if (supportsImageRenderingPixelated) {
            var vtxf_dpr = window.devicePixelRatio;
            while (vtxf_dpr >= 2.0) { vtxf_dpr /= 2.0; }
            viewer.resolutionScale = vtxf_dpr;
        }

        this.DataSource = null;
        // viewer.dataSources.removeAll();
        this.DataSource = this.DataSource || new Cesium.CustomDataSource('turnEntities');
        const dataList = [];
        let j = 0;
        for (let i = 0; i < target.length; i++) {
            const ii = i % 5;
            if (i < 5) {
                j = 1;
            }
            if (5 <= i) {
                j = 2;
            }
            if (10 <= i) {
                j = 3;
            }
            if (15 <= i) {
                j = 4;
            }
            dataList.push({
                layers: [
                    {
                        id: 1,
                        img: "",
                        color: Cesium.Color.BLUEVIOLET,
                        lon: 107.000001 + 0.0004 * ii,
                        lat: 34 + 0.0004 * j,
                        height: 1800,
                        length: 120,
                        uplength: 0,
                    },
                    {
                        id: 2,
                        img: "",
                        color: Cesium.Color.BROWN,
                        lon: 107.000001 + 0.0004 * ii,
                        lat: 34 + 0.0004 * j,
                        height: 1910,
                        length: 100,
                    },
                    {
                        id: 3,
                        img: "",
                        color: Cesium.Color.CADETBLUE,
                        lon: 107.000001 + 0.0004 * ii,
                        lat: 34 + 0.0004 * j,
                        height: 2000,
                        length: 80,
                    },
                    {
                        id: 4,
                        img: "",
                        color: Cesium.Color.CHOCOLATE,
                        lon: 107.000001 + 0.0004 * ii,
                        lat: 34 + 0.0004 * j,
                        height: 2100,
                        length: 120,
                    },
                    {
                        id: 5,
                        img: "",
                        color: Cesium.Color.DARKORANGE,
                        lon: 107.000001 + 0.0004 * ii,
                        lat: 34 + 0.0004 * j,
                        height: 2170,
                        length: 20,
                    },
                ],
            });
        }

        if (dataList.length) {
            for (let i = 0; i < dataList.length; i++) {
                const layerLit = dataList[i];
                if (layerLit && layerLit.layers.length) {
                    for (let j = 0; j < layerLit.layers.length; j++) {
                        const geo = layerLit.layers[j];
                        this.drawSingleCylinder(geo)
                    }
                }
            }
        }
        viewer.dataSources.add(this.DataSource);
        viewer.flyTo(this.DataSource);
    }
    drawSingleCylinder(data) {
        this.DataSource.entities.add({
            position: Cesium.Cartesian3.fromDegrees(data.lon, data.lat, data.height),
            cylinder: {
                length: data.length,
                topRadius: 10,
                bottomRadius: 10,
                material: data.color || Cesium.Color.BLUE
            }
            // id: options.id,
            // polylineVolume: {
            //     positions: positionsOut,
            //     clampToGround: true,
            //     shape: this.computeCircle(this.circleRadius + this.circleDifference * 2),
            //     // material: Cesium.Color.RED.withAlpha(0.6),
            //     material: new Cesium.ImageMaterialProperty({
            //         image: "../../../asset/materialImg/gongye.jpg", // require("./img/gongye.jpg"), //图片
            //         repeat: new Cesium.Cartesian2(10, 2), //s,t方向重复次数
            //     }), //图片材质
            //     // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(-3000.0, 2000.0),
            // },
        });
    }
    changeProfileStyle(id) {
        let entBox = null;
        const poOBjArrsource = viewer.dataSources.getByName("turnEntities") || [];
        if (poOBjArrsource.length) {
            entBox = poOBjArrsource[0].entities.getById(
                "zhuankong" + id
            );
            // 设置线的颜色
            entBox.billboard.image = require("../../assets/materialImg/turn2.png")
        }
        return entBox

    }
    backOriginalStyle(id) {
        const poOBjArrsource = viewer.dataSources.getByName("turnEntities") || [];
        if (poOBjArrsource.length) {
            const entBox = poOBjArrsource[0].entities.getById(
                "zhuankong" + id
            );
            // 设置线的颜色
            entBox.billboard.image = require("../../assets/materialImg/turn1.png")
        }

    }
    hideSource() {
        const poOBjArrsource = viewer.dataSources.getByName("LablesEntities") || [];
        poOBjArrsource[0].show = false
    }
    showSource() {
        const poOBjArrsource = viewer.dataSources.getByName("LablesEntities") || [];
        poOBjArrsource[0].show = true
    }
}